# Copyright (c) 2009 Daropia Project (www.daropia.org)
#
# This file is part of Daropia and is available under the terms
# of the GNU General Public Licence version 2.0 or later.


# Handle forum operations
# Mounted to /admin

class AdminController < Controller

	def index
		require_login	
		require_admin
	
        @title = "Espace administrateur"
    end
    
    def show_characters
    	require_login	
		require_admin
    	
    	@characters = Character.all
    end
    
    def show_users
    	require_login	
		require_admin
    	
    	@users = Array.new	
    	users = DB.execute("select * from users")
    	users.each do |user|
    		@users << user
    	end    	
    end
    
    def manage_user(id_user)  
    	require_login	
		require_admin
    	
    	@users = Array.new
    	users = DB.execute("select * from users")
    	users.each do |user|
    		@users << user
    	end    	
    	@user = @users[id_user.to_i - 1]
    	
   		@title = "Gestion de " + @user[1]
   	end
   	
   	def change_status(id_user)
   		require_login	
		require_admin
   		
   		puts request['status']
    	
    	DB.transaction do |db|
	    	if request['status'] == "membre"
		    	db.execute("UPDATE users SET status='membre' WHERE id = ?",  id_user)
	    	elsif request['status'] == "admin"
			    db.execute("UPDATE users SET status='admin' WHERE id = ?",  id_user)
	    	end
	    end
    	
    	
    	redirect r(:show_users)
    
    end		
   		
   		
   		
    
    def manage_character(id_character)
    	require_login	
		require_admin
    	
    	@character = Character[:id => id_character]
   		@name =  @character.name
   		@title = "Gestion de " + @name
   	end

end